﻿using DAO.DAOUtils;
using DAO.IAuthorizationDAO;
using DTO.Authorization;
using DTO.DTOUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO.AuthorizationDAO
{
    public class AuthorityDAO : IAuthorityDAO
    {
        public bool Insert(DTO.Authorization.AuthorityDTO authority)
        {
            var parameter = GetListParameters(authority);
            return SQLUtils.ExcuteNoneQuery("AuthorityInsert", ref parameter);
        }

        public bool DeleteByUserId(string userId)
        {
            var parameter = Parameter.CreateList(new Parameter(DTOConstant.UserId, userId));
            return SQLUtils.ExcuteNoneQuery("AuthorityeleteBuUserId", ref parameter);
        }

        public bool DeleteByRoleId(string roleId)
        {
            var parameter = Parameter.CreateList(new Parameter(DTOConstant.RoleId, roleId));
            return SQLUtils.ExcuteNoneQuery("AuthorityDeleteByRoleId", ref parameter);
        }

        public bool UpdateByParam(DTO.Authorization.AuthorityDTO authority, bool active)
        {
            var parameter = GetListParameters(authority);
            parameter.Add(new Parameter("@Active", active));
            return SQLUtils.ExcuteNoneQuery("AuthorityUpdateByParam", ref parameter);
        }

        public System.Data.DataTable GetRoleIdByUserId(string userId)
        {
            var parameter = Parameter.CreateList(new Parameter(DTOConstant.UserId, userId));
            return SQLUtils.ExcuteQuery("AuthorityGetRoleIdByUserId", ref parameter);
        }

        public System.Data.DataTable GetUserIdByRoleId(string roleId)
        {
            var parameter = Parameter.CreateList(new Parameter(DTOConstant.RoleId, roleId));
            return SQLUtils.ExcuteQuery("AuthorityGetUserIdByRoleId", ref parameter);
        }

        public System.Data.DataTable GetAll()
        {
            var parameter = Parameter.CreateList();
            return SQLUtils.ExcuteQuery("AuthorityGetAll", ref parameter);
        }

        public List<Parameter> GetListParameters(AuthorityDTO authority)
        {
            return Parameter.CreateList(
                    new Parameter(DTOConstant.RoleId, authority.RoleId),
                    new Parameter(DTOConstant.UserId, authority.UserId)
                );
        }
    }
}
